package com.ios.service.impl;

import com.ios.dao.DisablerUserMapper;
import com.ios.dao.QueryLocationMapper;
import com.ios.dao.RelativeUserMapper;
import com.ios.dto.QueryLocationExecution;
import com.ios.entity.QueryLocation;
import com.ios.enums.LocationStateEnum;
import com.ios.service.IQueryLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QueryLocationServiceImpl implements IQueryLocationService{
    @Autowired
    private QueryLocationMapper queryLocationMapper;
    @Autowired
    private DisablerUserMapper disablerUserMapper;
    @Autowired
    private RelativeUserMapper relativeUserMapper;

    @Override
    public QueryLocationExecution AddQueryLocation(int disabler_id, int relative_id, int status) {
        if (disabler_id==-1 || relative_id==-1 || status==-1){
            return new QueryLocationExecution(LocationStateEnum.NULL_PARAM);
        }else if (!CheckDisablerId(disabler_id) || !CheckRelativeId(relative_id)){
            return new QueryLocationExecution(LocationStateEnum.NULL_USER);
        }else {
            try{
                QueryLocation queryLocation = new QueryLocation();
                queryLocation.setDisablerId(disabler_id);
                queryLocation.setRelativeId(relative_id);
                queryLocation.setStatus(status);
                queryLocation.setCreateTime(new Date());
                queryLocationMapper.insert(queryLocation);
                return new QueryLocationExecution(LocationStateEnum.SUCCESS_ADDQUERY,queryLocation);
            }catch (Exception e){
                return new QueryLocationExecution(LocationStateEnum.FAIL_ADDQUERY);
            }
        }
    }

    @Override
    public QueryLocationExecution QueryLocationByDisablerId(int disabler_id,int status) {
        if (disabler_id==-1 || status==-1){
            return new QueryLocationExecution(LocationStateEnum.NULL_PARAM);
        }else if(!CheckDisablerId(disabler_id)){
            return new QueryLocationExecution(LocationStateEnum.NULL_USER);
        }else {
            try{
                List<QueryLocation> queryLocations = queryLocationMapper.selectByDisabler(disabler_id,status);
                return new QueryLocationExecution(LocationStateEnum.SUCCESS_QUERY_QUERY,queryLocations);
            }catch (Exception e){
                return new QueryLocationExecution(LocationStateEnum.FAIL_QUERY_QUERY);
            }
        }
    }

    @Override
    public QueryLocationExecution QueryLocationByRelativeId(int relative_id,int status) {
        if (relative_id==-1 || status==-1){
            return new QueryLocationExecution(LocationStateEnum.NULL_PARAM);
        }else if (!CheckRelativeId(relative_id)){
            return new QueryLocationExecution(LocationStateEnum.NULL_USER);
        }else {
            try{
                List<QueryLocation> queryLocations = queryLocationMapper.selectByRelative(relative_id,status);
                return new QueryLocationExecution(LocationStateEnum.SUCCESS_QUERY_QUERY,queryLocations);
            }catch (Exception e){
                return new QueryLocationExecution(LocationStateEnum.FAIL_QUERY_QUERY);
            }
        }
    }

    /*判断用户是否存在*/
    private boolean CheckDisablerId(Integer disablerId){
        List<Integer > AllId = disablerUserMapper.getAllDisablerId();
        for (Integer id : AllId){
            if (id==disablerId){
                return true;
            }
        }
        return false;
    }

    private boolean CheckRelativeId(Integer relativeId){
        List<Integer > AllId = relativeUserMapper.queryAllId();
        for (Integer id : AllId){
            if (id==relativeId){
                return true;
            }
        }
        return false;
    }
}
